package com.cuitrip.component;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;

/**
 * Created by MiJack on 2016/1/19.
 */
public class LabelView extends AppCompatTextView {
    private Label label;

    public LabelView(Context context) {
        super(context);
    }

    public LabelView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public Label getTag() {
        return label;
    }

    public void setLabel(Label label) {
        this.label = label;
        resetView();
    }

    private void resetView() {
        if (label == null) {
            return;
        }
        setText(label.getTextContent());
        if (checkColor(label.getTextColor())) {
            setTextColor(Color.parseColor("#"+label.getTextColor()));
        }
        int horizontalPadding = (int) (label.getHorizontalPadding() * getResources().getDisplayMetrics().density);
        int verticalPadding = (int) (label.getVerticalPadding() * getResources().getDisplayMetrics().density);
        setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
        setTextSize(label.getTextSize());
        int density = (int) getResources().getDisplayMetrics().density;
        int borderWidth = (int) (label.getBorderWidth() * density);
        int inR = (int) (label.getBorderRadius() * density);
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadius(inR);
        if (checkColor(label.getBorderColor())) {
            drawable.setStroke(borderWidth, Color.parseColor("#"+label.getBorderColor()));
        }
        if (checkColor(label.getTextBackground())) {
            drawable.setColor(Color.parseColor("#"+label.getTextBackground()));
        }
        setBackgroundDrawable(drawable);
    }

    private boolean checkColor(String color) {
        if (TextUtils.isEmpty(color)){return  false;}
        if (color.length()!=6){return  false;}
        return true;
    }
}
